using UnityEngine;
using System.Collections;

public class CheckpointManager : MonoBehaviour {

    private FadeDemo fade;
    public Transform lastRoom;
    public Transform currentRoom;
    private Transform firstRoom;
	private CharacterManager player;
    private DataManager manager;
    private Vector3 position;
    public GameObject NPC;
    private bool blackCube;
    private float timer;
    
    public static CheckpointManager getInstance;

    void Awake()
    {
        getInstance = this;
    }

	void Start()
	{
        position = transform.position;
        manager = GameObject.FindGameObjectWithTag("_Manager").GetComponent<DataManager>();
   
        fade = gameObject.AddComponent("FadeDemo") as FadeDemo;
        
		player = gameObject.GetComponent<CharacterManager>();

	}
	
	void OnTriggerEnter(Collider obj)
	{
        if(obj.name == "Floor")
		{
            if (currentRoom != null)
            {
                if (obj.GetComponent<RoomInfo>().Father.name != currentRoom.name)
                {
                    if (manager)
                    manager.receivedRoom(obj.GetComponent<RoomInfo>().Father.name);

                    if(!blackCube)
                    fade.FadeRoom(obj.GetComponent<RoomInfo>().Father, currentRoom);

                    lastRoom = currentRoom;
                    currentRoom = obj.GetComponent<RoomInfo>().Father;
                }
            }
            return;
		}

        //if (obj.name == "BlackCube")
        //{
        //    if(NPC)
        //        NPC.SendMessage("begin", SendMessageOptions.DontRequireReceiver);
        //    blackCube = true;
        //    Destroy(obj.GetComponent<BoxCollider>());
        //    obj.gameObject.AddComponent("DoorDestroy");
        //    obj.GetComponent<DoorDestroy>().amount = 1f;

        //    manager.gameObject.GetComponent<FadeInScene>().BlackCube(currentRoom.name);
        //}
	}

    void OnTriggerStay(Collider obj)
    {
        if (currentRoom == null)
        {
            currentRoom = obj.GetComponent<RoomInfo>().Father;
            this.firstRoom = obj.GetComponent<RoomInfo>().Father;
            
            if (manager)
            manager.receivedRoom(obj.GetComponent<RoomInfo>().Father.name);
        }
    }

    public void goToCheckpoint()
    {
        transform.position = position;
        renderer.material.color = gameObject.GetComponent<ColorManager>().myColor;
        if (currentRoom.name != firstRoom.name)
        {
            if (!blackCube)
            fade.FadeRoom(firstRoom, currentRoom);
            currentRoom = firstRoom;
        }

        StartCoroutine(activeCharacter());
    }

    void Update()
    {
        if (blackCube)
        {
            timer += Time.deltaTime;

            if (timer >= 15)
            {
                timer = 0;
                blackCube = false;
                manager.gameObject.GetComponent<FadeInScene>().ReturnBlackCube(currentRoom.name);
            }
        }
    }

    IEnumerator activeCharacter()
    {
        yield return new WaitForSeconds(0.4f);
        gameObject.GetComponent<CharacterManager>().ActiveElement(CharacterManager.CharacterFunctions.MOVEMENT);
        gameObject.GetComponent<CharacterManager>().ActiveElement(CharacterManager.CharacterFunctions.DROP);
        gameObject.GetComponent<CharacterManager>().ActiveElement(CharacterManager.CharacterFunctions.CHARACTERCOLLISION);
        rigidbody.isKinematic = false;
    }


}

